//
//  XHEditViewController.m
//  Big Show
//
//  Created by 赵晓航 on 15-5-13.
//  Copyright (c) 2015年 Big_Q. All rights reserved.
//

#import "XHEditViewController.h"
#import "XHEditTableViewCell.h"
#import "XHEditxiangViewController.h"
#import "XHNetworkHandle.h"
#import "MJRefresh.h"

@interface XHEditViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
// 容纳tableView数据的数组
@property (nonatomic, retain) NSMutableArray *mArr;
// 页码控制器(点点)的属性
@property (nonatomic, retain) UIPageControl *pagecontrol;
// scrollView(轮播图的属性)
@property (nonatomic, retain)UIScrollView *scrollView;
// 定时器的属性
@property (nonatomic, retain) NSTimer *timer;

@property (nonatomic, retain) NSMutableDictionary *mDic;

@property (nonatomic, retain) UIActivityIndicatorView *activi;

@property (nonatomic, retain) UIView *passView;

@property (nonatomic, retain) UIImageView *img;

@property (nonatomic, assign) NSInteger page;

@end
@implementation XHEditViewController
- (void)dealloc
{
    [_activi release];
    [_passView release];
    [_mArr release];
    [_mDic release];
    [_img release];
    [_tableView release];
    [_pagecontrol release];
    [_scrollView release];
    [_timer release];
    [super dealloc];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"发现";
    self.navigationController.navigationBar.translucent = NO;
    
    self.page = 30;
    
    [self createImageView];
    [self createTableView];
    [self createscrollView];
    [self createRefresh];
    [self createPassView];
    [self creatFootRefresh];
    
    
    [self getData];
    
    // 创建定时器
   self.timer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(haha) userInfo:nil repeats:YES];
    
}

- (void) createPassView
{
    self.passView = [[UIView alloc] initWithFrame:self.view.bounds];
    _passView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.97];
    [self.view addSubview:_passView];
    [_passView release];
    
    self.activi = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    _activi.center = _passView.center;
//    _activi.backgroundColor = [UIColor yellowColor];
    _activi.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
    [_passView addSubview:_activi];
    
    [_activi startAnimating];
    
    [_activi release];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 180, 40)];
    label.text = @"      玩命加载中...";
//    label.backgroundColor = [UIColor lightGrayColor];
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    [_activi addSubview:label];
    [label release];
    
    
}






// 封装AFN的调用
// 参数1: 网络揭露
// 参数2: Cookie
// 参数3: 返回数据的Block 参数result中是网络请求的返回数据
- (void)getData
{
    [XHNetworkHandle getDataWithURL:@"http://a1.go2yd.com/Website/channel/news-list-for-channel?appid=dress&cend=30&channel_id=u130&cstart=0&cv=1.1.1&distribution=com.apple.appstore&fields%5B%5D=title&fields%5B%5D=url&fields%5B%5D=source&fields%5B%5D=date&fields%5B%5D=image&fields%5B%5D=image_urls&fields%5B%5D=comment_count&fields%5B%5D=like&infinite=true&net=wifi&platform=0&refresh=0&version=010904" cookie:@"JSESSIONID=7tFm_0dR9sFG5Bm0IAnlqw" completion:^(id result) {
        // result为请求接口的返回数据
        self.mDic = result;
        self.mArr = [_mDic objectForKey:@"result"];
        [_tableView reloadData];
        
        [_passView removeFromSuperview];
        [_activi stopAnimating];
        
        self.page = 30;
    }];
    
}


-(void)creatFootRefresh
{
    
    [self.tableView addFooterWithCallback:^{
       
        NSString *url = [NSString stringWithFormat:@"http://a1.go2yd.com/Website/channel/news-list-for-channel?appid=dress&cend=%ld&channel_id=u130&cstart=%ld&cv=1.1.1&distribution=com.apple.appstore&fields=title&fields=url&fields=source&fields=date&fields=image&fields=image_urls&fields=comment_count&fields=like&infinite=true&net=wifi&platform=0&refresh=0&version=010904",self.page + 30, self.page];
        self.page += 30;
        
        [XHNetworkHandle getDataWithURL:url cookie:@"JSESSIONID=7tFm_0dR9sFG5Bm0IAnlqw" completion:^(id result) {
            // result为请求接口的返回数据
            self.mDic = result;
            NSArray *arr = [_mDic objectForKey:@"result"];
            
            for (NSDictionary *dic in arr) {
                [self.mArr addObject:dic];
            }
            
            [_tableView reloadData];
            
            [self.tableView footerEndRefreshing];
        }];
        
        
        
        
    }];
    
}


- (void)createImageView
{
    self.img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.img.image = [UIImage imageNamed:@"u=2916742164,3184761356&fm=21&gp=0.jpg"];
    [self.view addSubview:_img];
    [_img release];
}


- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 113) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.tableView.rowHeight = 160;
    [self.view addSubview:_tableView];
    [_tableView release];
    
    [_tableView registerClass:[XHEditTableViewCell class] forCellReuseIdentifier:@"cell"];
}

- (void)createRefresh
{
    [self.mArr removeAllObjects];
    
    [self.tableView addHeaderWithCallback:^{
        
        [self getData];
        
        [self.tableView headerEndRefreshing];
        
    }];
    
}
-(void)createscrollView
{
        
    
    // 创建一个大ScrollView实现轮播图
    self.scrollView = [[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)]autorelease];
    // 偏移量
    _scrollView.contentOffset = CGPointMake(self.view.frame.size.width, 0);
//        scrollView.backgroundColor = [UIColor redColor];
    // 视图个数
    _scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 6, 200);
    // 视图滑动协议
    _scrollView.delegate = self;
    // 关闭视图弹动效果
    _scrollView.bounces = NO;
    
    // 是否可以整页翻动
    _scrollView.pagingEnabled = YES;
    // 让scrollView写在tableView上方
    _tableView.tableHeaderView = _scrollView;

    
    // 第4照片(用于转换)
    UIImageView *bb = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    bb.image = [UIImage imageNamed:@"a.jpg"];
    // ***第4照片放第一位置
    [_scrollView addSubview:bb];
    [bb release];
    
    // 第1照片
    UIImageView *b1 = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, 200)];
    b1.image = [UIImage imageNamed:@"a1.jpg"];
    // 放在第二位置
    [_scrollView addSubview:b1];
    [b1 release];
    
    // 第2照片
    UIImageView *b2 = [[UIImageView alloc]initWithFrame:CGRectMake(2 * self.view.frame.size.width, 0, self.view.frame.size.width , 200)];
    b2.image = [UIImage imageNamed:@"a2.jpg"];
    // 放在第三位置
    [_scrollView addSubview:b2];
    [b2 release];
    
    // 第3照片
    UIImageView *b3 = [[UIImageView alloc] initWithFrame:CGRectMake(3 * self.view.frame.size.width, 0, self.view.frame.size.width, 200)];
    b3.image = [UIImage imageNamed:@"a3.jpg"];
    // 放在第四位置
    [_scrollView addSubview:b3];
    [b3 release];
    
    // 第4照片
    UIImageView *b4 = [[UIImageView alloc]initWithFrame:CGRectMake(4 * self.view.frame.size.width, 0, self.view.frame.size.width, 200)];
    b4.image = [UIImage imageNamed:@"a.jpg"];
    // 放在第五位置
    [_scrollView addSubview:b4];
    [b4 release];
    
    // ***第1照片(用于转换)
    UIImageView *bbb = [[UIImageView alloc]initWithFrame:CGRectMake(5 * self.view.frame.size.width, 0, self.view.frame.size.width, 200)];
    bbb.image = [UIImage imageNamed:@"a1.jpg"];
    // 放在第六位置
    [_scrollView addSubview:bbb];
    [bbb release];
    
    // 创建页码控制器(点点)
   self.pagecontrol = [[UIPageControl alloc]initWithFrame:CGRectMake(self.view.frame.size.width * 0.35, 180, 100, 20)];
    // 设置圆点
    _pagecontrol.numberOfPages = 4;
        //默认亮点下标
    _pagecontrol.currentPage = 0;
    // 未选中圆点颜色
     _pagecontrol.pageIndicatorTintColor = [UIColor whiteColor];
    // 选中圆点颜色
     _pagecontrol.currentPageIndicatorTintColor = [UIColor blackColor];
    // 绑定事件
        [ _pagecontrol addTarget:self action:@selector(page:) forControlEvents:UIControlEventValueChanged];
    [_tableView addSubview: _pagecontrol];
    [ _pagecontrol release];
}
// 绑定事件响应方法
- (void)page:(UIPageControl *)page
{
    //页码控制器 -->偏移量改变
    _scrollView.contentOffset = CGPointMake(_pagecontrol.currentPage * self.view.frame.size.width, 0);
}
// 轮播图效果
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //偏移量-->页码控制器
    _pagecontrol.currentPage = _scrollView.contentOffset.x / self.view.frame.size.width - 1;
    // 向右滑动返回第一张
    if (_scrollView.contentOffset.x == self.view.frame.size.width * 5) {
        _scrollView.contentOffset = CGPointMake(self.view.frame.size.width, 0);
        _pagecontrol.currentPage = 0;
        // 向左滑动跳到最后一张
    }else
        if (_scrollView.contentOffset.x == 0) {
            _scrollView.contentOffset = CGPointMake(self.view.frame.size.width * 4, 0);
            _pagecontrol.currentPage = 4;
        }
}
// 实现定时换图片的方法
-(void)haha
{
    // 利用偏移量使X横轴每次加375换图片
    _scrollView.contentOffset = CGPointMake(_scrollView.contentOffset.x + self.view.frame.size.width , 0);
    // 每次减1  使页码控制器完成切换图片
    _pagecontrol.currentPage = _scrollView.contentOffset.x / self.view.frame.size.width - 1;
    
    // 写if判断到第五张图时候  切换到第六张图(也就是第一张假图)  使左右都能滑动
    if (_scrollView.contentOffset.x == self.view.frame.size.width * 5  ) {
        _scrollView.contentOffset = CGPointMake(self.view.frame.size.width, 0);
        _pagecontrol.currentPage = 0;
    }
}






- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _mArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XHEditTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.backgroundColor = [UIColor clearColor];
    [cell setDic:[_mArr objectAtIndex:indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    XHEditxiangViewController *xiang = [[XHEditxiangViewController alloc] init];
    xiang.str = [_mArr[indexPath.row] objectForKey:@"url"];
    xiang.myDic = [_mArr objectAtIndex:indexPath.row];
    xiang.docid = [_mArr[indexPath.row] objectForKey:@"docid"];
    [self.navigationController pushViewController:xiang animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
